package com.san.oa.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.base.domain.DictDO;
import com.san.base.service.DictService;
import com.san.common.config.Constant;
import com.san.common.utils.FileUtil;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.oa.domain.NotifyDO;
import com.san.oa.domain.NotifyRecordDO;
import com.san.oa.service.NotifyRecordService;
import com.san.oa.service.NotifyService;
import com.san.oa.vo.NotifyVO;

/**
 * 通知通告
 *
 */

@Controller
@RequestMapping("/oa/notify")
public class NotifyController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(NotifyController.class);
	
	@Autowired
	private NotifyService notifyService;
	@Autowired
	private NotifyRecordService notifyRecordService;
	@Autowired
	private DictService dictService;

	@GetMapping()
	@RequiresPermissions("oa:notify:notify")
	String oaNotify() {
		return "oa/notify/view";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("oa:notify:notify")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<NotifyDO> notifyList = notifyService.list(query);
		int total = notifyService.count(query);
		PageUtils pageUtils = new PageUtils(notifyList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("oa:notify:add")
	String add() {
		return "oa/notify/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("oa:notify:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		try {
			NotifyDO notify = (NotifyDO)notifyService.getById(id);
			List<DictDO> dictDOS = dictService.listByType("oa_notify_type");
			model.addAttribute("oaNotifyTypes", dictDOS);
			model.addAttribute("notify", notify);
		} catch (Exception e) {
			logger.error("获取通知信息失败",e);
		}
		
		return "oa/notify/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("oa:notify:add")
	public R save(NotifyVO notify) {
		notify.setCreateUserId(getUserId());
		try {
			if (notifyService.save(notify) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("保存通知信息失败",e);
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/doEdit")
	@RequiresPermissions("oa:notify:edit")
	public R doEdit(NotifyVO notify) {
		notify.setUpdateUserId(getUserId());
		try {
			notifyService.doEdit(notify);
			return R.ok();
		} catch (Exception e) {
			logger.error("更新通知信息失败",e);
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("oa:notify:remove")
	public R remove(Long id) {
		try {
			if (notifyService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("删除通知信息失败",e);
		}
		return R.error();
	}

	/**
	 * 批量删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("oa:notify:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		try {
			notifyService.batchRemove(ids);
		} catch (Exception e) {
			logger.error("批量删除通知信息失败",e);
		}
		return R.ok();
	}

	@ResponseBody
	@GetMapping("/message")
	PageUtils message() {
		Map<String, Object> params = new HashMap<>(16);
		params.put("offset", 0);
		params.put("limit", 3);
		Query query = new Query(params);
		query.put("userId", getUserId());
		query.put("isRead", Constant.OA_NOTIFY_READ_NO);
		return notifyService.selfList(query);
	}

	@GetMapping("/selfNotify")
	String selefNotify() {
		return "oa/notify/selfNotify";
	}

	@ResponseBody
	@GetMapping("/selfList")
	PageUtils selfList(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		query.put("userId", getUserId());

		return notifyService.selfList(query);
	}

	@GetMapping("/read/{id}")
	@RequiresPermissions("oa:notify:edit")
	String read(@PathVariable("id") Long id, Model model) {
		try {
			NotifyDO notify = (NotifyDO)notifyService.getById(id);
			// 更改阅读状态
			NotifyRecordDO notifyRecordDO = new NotifyRecordDO();
			notifyRecordDO.setNotifyId(id);
			notifyRecordDO.setUserId(getUserId());
			notifyRecordDO.setReadDate(new Date());
			notifyRecordDO.setIsRead(Constant.OA_NOTIFY_READ_YES);
			notifyRecordService.changeRead(notifyRecordDO);
			model.addAttribute("notify", notify);
		} catch (Exception e) {
			logger.error("更新通知状态失败",e);
		}
		
		return "oa/notify/read";
	}

	/**
	 * 下载附件
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping("downloadFile/{id}")
	ResponseEntity<FileSystemResource> downloadFile(@PathVariable("id") Long id) {
		try {
			NotifyDO notify = (NotifyDO)notifyService.getById(id);
			if (null != notify && null != notify.getFilePath()) {
				return FileUtil.export(new File(notify.getFilePath()));
			}
		} catch (Exception e) {
			logger.error("获取通知信息失败",e);
		}
		
		return null;
	}

}
